package com.lin.subject.domain.handler.subject;

import com.lin.subject.commom.enums.IsDeletedFlagEnum;
import com.lin.subject.commom.enums.SubjectInfoTypeEnum;
import com.lin.subject.domain.convert.RadioSubjectConverter;
import com.lin.subject.domain.entity.SubjectAnswerBO;
import com.lin.subject.domain.entity.SubjectInfoBO;
import com.lin.subject.domain.entity.SubjectOptionBO;
import com.lin.subject.infra.basic.entity.SubjectRadio;
import com.lin.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/*
 * @Author Lin
 * @Description
 * @Date 2024/10/21
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    /**
     * 实际题目的插入
     *
     * @param subjectInfoBo
     */
    @Override
    public void insertSubject(SubjectInfoBO subjectInfoBo) {
        //单选题目的插入
        List<SubjectAnswerBO> optionList = subjectInfoBo.getOptionList();

        List<SubjectRadio> radioList = optionList.stream().map(item -> {
            SubjectRadio subjectRadio = new SubjectRadio();
            subjectRadio.setOptionContent(item.getOptionContent());
            subjectRadio.setOptionType(item.getOptionType());
            subjectRadio.setIsCorrect(item.getIsCorrect());
            subjectRadio.setSubjectId(subjectInfoBo.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            return subjectRadio;
        }).collect(Collectors.toList());

        subjectRadioService.insertList(radioList);
    }

    /**
     * 查询题目的选项及正确答案
     *
     * @param id
     * @return
     */
    @Override
    public SubjectOptionBO query(Long id) {
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(id);
        List<SubjectRadio> radioList = subjectRadioService.queryListByCondition(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOList = RadioSubjectConverter.INSTANCE.convertEntityToBoList(radioList);
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }

    /**
     * 删除题目
     *
     * @param id
     */
    @Override
    public void deleteSubject(Long id) {
        subjectRadioService.deleteSubject(id);
    }
}
